/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package morenoapp.components.thinkgraph.node;

import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolox.nodes.PComposite;
import java.awt.Color;
import morenoapp.components.thinkgraph.model.DisplayFilterListener;
import morenoapp.components.thinkgraph.model.DisplayMode;
import morenoapp.components.thinkgraph.model.DisplayFilter;
import morenoapp.model.data.MorenoQuestionFace;

/**
 *
 * @author root
 */
public class RelationshipNode extends PComposite implements PositionListener {
    //model
    RespondentNode from;
    RespondentNode to;
    MorenoQuestionFace question;
    DisplayFilter config;
    
    //visual
    VectorNode vectorNode;

    public RelationshipNode(RespondentNode _from, RespondentNode _to, MorenoQuestionFace _question, DisplayFilter _config) {
        super();
        
        this.from = _from;
        this.from.addPositionListener(this);
        
        this.to = _to;
        this.to.addPositionListener(this);

        this.question = _question;
        
        this.config = _config;
        this.config.addDisplayFilterLisneter(new DisplayFilterListener() {
            public void onDisplayFilterChanged() {
                applyDisplayMode();
            }
        });

        applyActualCoordinates();
    }

    private void applyActualCoordinates() {
        removeAllChildren();
//        joinLine = PPath.createLine(
//                (float) this.from.getX(), (float) this.from.getY(),
//                (float) this.to.getX(), (float) this.to.getY());
//        joinLine.setPaint(Color.BLUE);
//        addChild(joinLine);

        vectorNode = new VectorNode(
                this.from.getX(), this.from.getY(),
                this.to.getX(), this.to.getY(),
                17.0f,17.0f,
                vectorNode);
        addChild(vectorNode);

        applyDisplayMode();
    }

    private void applyDisplayMode(){
        DisplayMode displayMode = config.getDisplayModeForQuestion(question);
        switch(displayMode){
            case Normal:
                vectorNode.setPaint(question.getColor());
                break;
            case Gray:
                vectorNode.setPaint(displayMode.grayColor);
                break;
            case None:
                vectorNode.setPaint(null);
                break;
            default:
                throw new RuntimeException("Unknown display mode: "+displayMode); //NOI18N

        }
        
    }

    public void onPositionChange(PNode _movedNode) {
        applyActualCoordinates();
    }
}
